package com.ky.facescan.device.telpo;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.util.Pair;

import com.hello.scan.ScanTool;
import com.ky.facescan.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;


public class CodeScan {

    Handler mHandler = new Handler(Looper.myLooper());

    private static final Map<String, Pair<String, Integer>> sDevices;

    CodeScanCallback codeScanCallback;

    static {
        /*
         * 支持设备列表，若是没在支持列表数据机型，在已知串口号和波特率的情况下，可以直接传数据
         */
        sDevices = new HashMap<>();
        sDevices.put("TPS508", new Pair<>("/dev/ttyACM0", 115200));
        sDevices.put("TPS360", new Pair<>("/dev/ttyACM0", 115200));
        sDevices.put("TPS537", new Pair<>("/dev/ttyACM0", 115200));
        sDevices.put("F2", new Pair<>("/dev/ttyACM0", 115200));
        sDevices.put("D2", new Pair<>("/dev/ttyHSL0", 115200)); // D2串口模式
        //sDevices.put("D2", new Pair<>("/dev/ttyACM0", 115200)); // D2U转串模式
        sDevices.put("D2M", new Pair<>("/dev/ttyHSL0", 115200)); //串口模式
        sDevices.put("TPS980", new Pair<>("/dev/ttyS0", 115200));
        sDevices.put("TPS980P", new Pair<>("/dev/ttyS0", 115200));
        //sDevices.put("TPS980P", new Pair<>("/dev/ttyS0", 9600));
        sDevices.put("TPS530", new Pair<>("/dev/ttyUSB0", 115200));
        sDevices.put("TPS580P", new Pair<>("/dev/ttyHSL0", 115200));
    }

    private static volatile CodeScan singleton;

    public static CodeScan getInstance(Context context) {
        if (singleton == null) {
            synchronized (CodeScan.class) {
                if (singleton == null) {
                    singleton = new CodeScan(context);
                }
            }
        }
        return singleton;
    }

    public CodeScan(Context context) {
        if (!initScanTool(context)) {
            mHandler.post(() -> ToastUtils.show("该机型还没有适配"));
        } else {
            ScanTool.GET.playSound(true);
        }
    }

    public void setCodeScanCallback(CodeScanCallback codeScanCallback) {
        this.codeScanCallback = codeScanCallback;
    }

    public void stop() {
        ScanTool.GET.pauseReceiveData();
    }

    public void resume() {
        ScanTool.GET.resumeReceiveData();
    }

    public void destroy() {
        ScanTool.GET.release();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        singleton = null;
    }

    /**
     * 判断使用模式
     *
     * @return 返回true表示表示该机型已经适配，false表示该机型还没有适配
     */
    private boolean initScanTool(Context context) {
        for (String s : sDevices.keySet()) {
            if (s.equals(Build.MODEL)) {
                Pair<String, Integer> pair = sDevices.get(s);
                if (pair == null) continue;
                Log.e("initScanTool", "judgeModel == > " + s);
                Log.e("initScanTool", "path == > " + pair.first);
                Log.e("initScanTool", "baud rate == > " + pair.second);
                ScanTool.GET.initSerial(context, pair.first, pair.second, code -> {
                    mHandler.post(() -> {
                        if (codeScanCallback != null) {
                            codeScanCallback.codeResult(code);
                        }
                    });
                });
                return true;
            }
        }
        return false;
    }

    public interface CodeScanCallback {
        void codeResult(String code);
    }
}
